<?php

namespace App;

use App\Model;
use Laravel\Scout\Searchable;
use PhpParser\Builder;

class Post extends Model
{
    use Searchable;

    //定义搜索索引里的type
    public function searchableAs()
    {
        return "post";
    }

    //定义由那些字段需要搜索
    public function toSearchableArray()
    {
        return [
            'title' => $this->title,
            'content' => $this->content,
        ];
    }

    //关联用户
    public function user()
    {
        return $this->belongsTo('App\User', 'user_id', 'id');
    }

    //评论模型关联
    public function comments()
    {
        return $this->hasMany('App\Comment')->orderBy('created_at', 'desc');
    }

    //和当前用户赞的关联  单对单
    public function zan($user_id)
    {
        return $this->hasOne('\App\Zan')->where('user_id', $user_id);
    }

    //文章的所有赞 一对多
    public function zans()
    {
        return $this->hasMany('App\Zan');
    }

    //属于某个作者的文章
    public function scopeAuthorBy($query, $user_id)
    {
        return $query->where('user_id', $user_id);
    }

    //获取这个文章有多少个topic
    public function postTopics()
    {
        return $this->hasMany(\App\PostTopic::class, 'post_id');
    }

    //不属于某个专题的文章
    public function scopeTopicNotBy($query, $topic_id)
    {
        return $query->doesntHave('postTopics', 'and', function ($q) use ($topic_id) {
            $q->where('topic_id', $topic_id);
        });
    }

    protected static function  boot(){
        parent::boot();
        static::addGlobalScope("avaiable",function($builder){
            $builder->whereIn('status',[0,1]);
        });
    }
}
